const {
	default: store
} = require("../store")

var baseUrl = "http://192.168.28.18:9090"
// var baseUrl = "http://lgb24kcs.cn:9090"
// let envVersion = uni.getAccountInfoSync().miniProgram.envVersion;
// switch (envVersion) {
// 	case "develop": // 开发版
// 		// baseUrl = "http://testapplet.boyunxr.com"; //测试环境
// 		baseUrl = "https://127.0.0.1:9090"; //测试环境
// 		break;
// 	case "trial": // 体验版
// 		baseUrl = "http://testapplet.boyunxr.com"; //测试环境
// 		break;
// 	case "release": // 正式版
// 		baseUrl = "https://applet.boyunxr.com"; //正式环境
// }



const httpRequest = function(url, method = 'POST', params = {}, need_token = true, header = {
	'content-type': 'application/json;charset=utf-8'
}, timeout = 60000) {

	return new Promise((resolve, reject) => {
		if (need_token == true) {
			let auth_token = uni.getStorageSync('auth_token')
			header.Authorization = 'Bearer ' + auth_token
		}
		var request_url = baseUrl + url
		uni.request({
			url: request_url,
			method: method,
			data: params,
			header: header,
			dataType: 'json',
			timeout: timeout,
			success(res) {
				switch (res.data.code) {
					case 200:
						resolve(res.data.data)
						break;
					case 401:
						uni.showToast({
							title: "用户校验过期，请重新登录",
							icon: 'error',
							position: 'center',
							duration: 2500
						});
						uni.clearStorageSync('auth_token')
						uni.reLaunch({
							url: '/pages/index/index'
						})
						break;
					default:
						
						reject(res.data.message)
						break;
				}
			},
			fail(res) {
				uni.showToast({
					title: '服务异常',
					icon: 'error',
					duration: 2500
				});
			}

		})

	})
}

// const tips = function(title, time = 1500) {
// 	wx.showToast({
// 		title: title,
// 		icon: 'none',
// 		// image: '../../images/fail.png',
// 		duration: time,
// 		mask: false
// 	})
// }

exports.httpRequest = httpRequest
// exports.tips = tips
exports.baseUrl = baseUrl